<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="24">
          <!-- 搜索栏和添加商品 -->
          <el-input placeholder="请输入内容" class="search-input" clearable @clear="getGoods" v-model="queryInfo.query"><el-button slot="append" icon="el-icon-search" @click="getGoods"></el-button></el-input>
          <el-button type="primary" @click="goAddGood">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 数据展示表格 -->
      <el-table
        :data="goodsList"
        style="width: 100%"
        border
        stripe  
      >
        <el-table-column type="index" label="#" width="40"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          min-width="400"
        >
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格"
          width="80">
        </el-table-column>
        <el-table-column
          prop="goods_number"
          label="商品数量"
          width="80">
        </el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="180">
          <template slot-scope="scope">
            {{scope.row.add_time | timeFormat}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGood(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total=total
        background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name:'List',
  created () {
    this.getGoods()
  },
  data () {
    return {
      // 商品列表
      goodsList:[],
      // 请求参数
      queryInfo:{
        query:'',
        pagenum: 1,
        pagesize: 10
      },
      // 数据总数
      total:0
    }
  },
  methods: {
    // 请求商品列表
    async getGoods(){
      const{data:res}=await this.$http.get('goods',{params:this.queryInfo})
      if(res.meta.status!==200) return this.$message.error('请求商品列表失败')
      this.goodsList=res.data.goods
      this.total=res.data.total
    },
    // 切换每页显示条数
    handleSizeChange(newSize){
      this.queryInfo.pagesize=newSize
      this.getGoods()
    },
    // 切换页码
    handleCurrentChange(newNum){
      this.queryInfo.pagenum=newNum
      this.getGoods()
    },
    // 删除商品
    deleteGood(id){
      this.$confirm('确定要删除该商品吗','删除商品',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'waring'
      }).then(async ()=>{
        const{data:res}=await this.$http.delete(`goods/${id}`)
        if(res.meta.status!==200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        this.getGoods()
      }).catch(()=>{
        this.$message.info('已取消删除')
      })
    },
    // 跳转到添加商品页面
    goAddGood(){
      this.$router.push('/goods/add')
    },
  },
}
</script>

<style lang="less" scoped>
.search-input{
  margin-right: 15px;
  width: 30%;
}
</style>